package com.pms.eap.util;

import javax.annotation.Resource;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.espirit.eap.conversion.BigDecimalConverter;
import com.espirit.eap.manager.DefaultEntityInterceptor;
import com.espirit.eap.manager.EntityInterceptor;
import com.googlecode.cswish.struts.Initialization;
import com.googlecode.cswish.struts.conversion.DefaultDateConverter;
import com.googlecode.cswish.struts.spring.ObjectFactory;

@Service
@Lazy(value=false)
public class BizSystemStartupManager implements Initialization {

	@Resource
	private ObjectFactory objectFactory;
	
	@Override
	public void initSite() {
		// data converter
		DefaultDateConverter defaultDateConverter = objectFactory.getBean(DefaultDateConverter.class, true);
		BigDecimalConverter bigDecimalConverter = objectFactory.getBean(BigDecimalConverter.class, true);		
		// model search callback
		
		SystemLogRegularService espiritSystemLogRegularService = objectFactory.getBean(SystemLogRegularService.class, true);
		espiritSystemLogRegularService.registerSystemLogRegular();
		
		// company license
//		CompanyService companyService = objectFactory.getBean(CompanyService.class, true);
//		companyService.changeDefaultCompanyName();
		
		// Entiry Interceptor
		EntityInterceptor entityInterceptor = objectFactory.getBean(EntityInterceptor.class, true);
		DefaultEntityInterceptor defaultInterceptor = objectFactory.getBean(DefaultEntityInterceptor.class, true);
		entityInterceptor.setInterceptor(defaultInterceptor);
		
		// work flow
//		registerWFTemplate();
	}
	/*
	private void registerWFTemplate() {
		WFTemplateService wfTemplateService = objectFactory.getBean(WFTemplateService.class, true);
		ModelService modelService = objectFactory.getBean(ModelService.class, true);
		
		Class [] srcItems = new Class[] {SalesOrder.class
				,PurchaseOrder.class
				,PackingList.class
				,RMAFromCustomer.class
				,RMAToSupplier.class
				,LeaveAppForm.class
				,Announcement.class
				,Customer.class
				,Supplier.class
				,Payment.class
				,Receipt.class
				,Delivery.class
				,Quote.class
				,Delivery.class
				,ChargeOrder.class
				,Invoice.class
				,PurchaseInvoice.class};
		
		for(Class clazz : srcItems){
			wfTemplateService.register(clazz, modelService.getI18nValue(clazz));
		}
		
		WFInstanceService wfInstanceService = objectFactory.getBean(WFInstanceService.class, true);
		
		wfInstanceService.registerNotice(new Executor<Object, Object[]>() {
			
			@Override
			public Object execute(Object[] parameters) {
				WorkflowAware workflowAware = (WorkflowAware)parameters[0];
				LoginUser to = (LoginUser)parameters[1];
				UserService userService = objectFactory.getBean(UserService.class, true);
				User toUser = userService.getUserByLoginUser(to.getName());
				
				UserNoticeService userNoticeService = objectFactory.getBean(UserNoticeService.class, true);
				if (SalesOrder.class.isAssignableFrom(workflowAware.getClass()) ){
					userNoticeService.increase(toUser, "approvingSI");
				} else if (PurchaseOrder.class.isAssignableFrom(workflowAware.getClass()) ){
					userNoticeService.increase(toUser, "approvingPI");
				} else if (PackingList.class.isAssignableFrom(workflowAware.getClass()) ){
					userNoticeService.increase(toUser, "approvingPLI");
				} 
				return null;
			}
		});
	}
	*/
    final String[] productName = new String[] {"espirit", "pms"};
	@Override
	public String[] getProductName() {
		return productName;
	}
}
